﻿using System;
using System.Xml;
using System.Web.UI;
using System.Collections.Generic;
using Rssc.administrator.app_data;

namespace Rssc.administrator
{
    public partial class uc_nav : System.Web.UI.UserControl
    {
        private List<nav> _listNav;
        private string _class = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            _listNav = NavLevel1_Load(Server.MapPath("app_data/nav.xml"));
            ltrNav.Text = Menu_Load();
        }

        private string Menu_Load()
        {
            string _result = string.Empty;

            _result += "<ul class=\"container_12\">\n";
            foreach (var item in _listNav)
            {
                switch (item._name)
                {
                    case "HOME":
                        if (item._level == 1)
                        {
                            _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\" title=\"" + item._name + "\">" + item._name + "</a>";
                            _result += SubNavLevel2(GetList(item._id, item._level + 1));
                            _result += "</li>\n";
                        }
                        break;
                    case "WRITE":
                        if (item._level == 1)
                        {
                            _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\" title=\"" + item._name + "\">" + item._name + "</a>\n";
                            _result += SubNavLevel2(GetList(item._id, 2));
                            _result += "</li>\n";
                        }
                        break;
                    case "COMMENTS":
                        if (item._level == 1)
                        {
                            _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\" title=\"" + item._name + "\">" + item._name + "</a>";
                            _result += SubNavLevel2(GetList(item._id, 2));
                            _result += "</li>\n";
                        }
                        break;
                    case "MEDIAS":
                        if (item._level == 1)
                        {
                            _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\" title=\"" + item._name + "\">" + item._name + "</a>";
                            _result += SubNavLevel2(GetList(item._id, 2));
                            _result += "</li>\n";
                        }
                        break;
                    case "USERS":
                        if (item._level == 1)
                        {
                            _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\" title=\"" + item._name + "\">" + item._name + "</a>";
                            _result += SubNavLevel2(GetList(item._id, 2));
                            _result += "</li>\n";
                        }
                        break;
                    case "STATS":
                        if (item._level == 1)
                        {
                            _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\" title=\"" + item._name + "\">" + item._name + "</a>";
                            _result += SubNavLevel2(GetList(item._id, 2));
                            _result += "</li>\n";
                        }
                        break;
                    case "SETTINGS":
                        if (item._level == 1)
                        {
                            _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\" title=\"" + item._name + "\">" + item._name + "</a>";
                            _result += SubNavLevel2(GetList(item._id, 2));
                            _result += "</li>\n";
                        }
                        break;
                    case "BACKUP": //BACKUP
                        if (item._level == 1)
                        {
                            _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\" title=\"" + item._name + "\">" + item._name + "</a>";
                            _result += SubNavLevel2(GetList(item._id, 2));
                            _result += "</li>\n";
                        }
                        break;
                    default:
                        break;
                }
            }
            return _result + "</ul>\n";
        }
                      
        private List<nav> NavLevel1_Load(string path)
        {
            List<nav> _result = new List<nav>();
            XmlTextReader _reader = new XmlTextReader(path);
            _reader.WhitespaceHandling = WhitespaceHandling.None;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(_reader);
            _reader.Close();

            XmlNodeList _nodes = xmlDoc.GetElementsByTagName("menu");
            for (int i = 0; i < _nodes.Count; i++)
            {
                nav _node = new nav();
                _node._id = Convert.ToInt16(_nodes[i].Attributes["id"].Value);
                _node._parent_id = Convert.ToInt16(_nodes[i].Attributes["parent_id"].Value);
                _node._level = Convert.ToInt16(_nodes[i].Attributes["level"].Value);
                _node._href = _nodes[i].Attributes["href"].Value.ToString();
                _node._class = _nodes[i].Attributes["class"].Value.ToString();

                _node._name = _nodes[i].InnerText;
                _result.Add(_node);
            }

            return _result;
        }

        private List<nav> GetList(int _id, int _level)
        {
            List<nav> _result = new List<nav>();
            foreach(var item in _listNav)
            {
                if (item._parent_id == _id && item._level == _level)
                {
                    _result.Add(item);
                }
            }
            return _result;
        }

        private string SubNavLevel2(List<nav> _list)
        {
            string _result = string.Empty;
            if (_list.Count != 0)
            {
                _result += "<ul>\n";
                foreach (var item in _list)
                {
                    _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\" title=\"" + item._name + "\">" + item._name + "</a>\n";
                    _result += SubNavLevel3(GetList(item._id, 3));
                    _result += "</li>\n";
                }
                _result += "</ul>\n";
            }
            return _result;
        }

        private string SubNavLevel3(List<nav> _list)
        {
            string _result = string.Empty;
            int i = 1;
            if (_list.Count != 0)
            {
                _result += "<div class=\"menu\">\n";
                _result += "<img alt=\"\" src=\"images/menu-open-arrow.png\" width=\"16\" height=\"16\" />";
                _result += "<ul>\n";
                foreach (var item in _list)
                {
                    _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\">" + item._name + "</a>\n";
                    _result += SubNavLevel4(GetList(item._id, 4));
                    _result += "</li>\n";
                    if (i==2 || i==5)
                    {
                        _result += "<li class=\"sep\"></li>";
                    }
                    i++;
                }
                _result += "</ul>\n";
                _result += "</div>\n";
            }      
            return _result;
        }

        private string SubNavLevel4(List<nav> _list)
        {
            string _result = string.Empty;
            if (_list.Count != 0)
            {
                _result += "<ul>\n";
                foreach(var item in _list)
                {
                    _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\">" + item._name + "</a>\n";
                    _result += SubNavLevel5(GetList(item._id, item._level + 1));
                    _result += "</li>\n";
                }
                _result += "</ul>\n";
            }
            return _result;
        }

        private string SubNavLevel5(List<nav> _list)
        {
            string _result = string.Empty;
            if (_list.Count != 0)
            {
                _result += "<ul>\n";
                foreach (var item in _list)
                {
                    _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\">" + item._name + "</a>\n";
                    _result += SubNavLevel6(GetList(item._id, 6));
                    _result += "</li>\n";
                }
                _result += "</ul>\n";
            }
            return _result;
        }

        private string SubNavLevel6(List<nav> _list)
        {
            string _result = string.Empty;
            if (_list.Count != 0)
            {
                _result += "<ul>\n";
                foreach (var item in _list)
                {
                    _result += "<li " + GetClass(item._class) + "><a href=\"" + item._href + "\">" + item._name + "</a>\n";
                    _result += "</li>\n";
                }
                _result += "</ul>\n";
            }
            return _result;
        }

        private string GetClass(string _class)
        {
            if (_class != string.Empty)
                return "class=\"" + _class + "\"";
            return string.Empty;
        }
    }
}